package com.gxa.controller;

import com.gxa.common.annotation.RequirePermission;
import com.gxa.domain.dto.OrderDto;
import com.gxa.domain.dto.SalegetData;
import com.gxa.domain.SaleSetData;
import com.gxa.result.Result;
import com.gxa.service.Saleservice;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@RestController
@CrossOrigin(origins = "*")
@Tag(name = "销售管理", description = "销售信息管理相关接口")
public class SaleControl {

    @Autowired
    private Saleservice saleService;

    private SaleSetData saleSetData = new SaleSetData();
    private List<Integer> count;
    final List<String> months = Arrays.asList(
            "1月", "2月", "3月", "4月", "5月", "6月",
            "7月", "8月", "9月", "10月", "11月", "12月"
    );

    @Operation(summary = "销售查询")
    @RequirePermission("报表查看")
    @PostMapping("/sale")
    public Result sale(@RequestBody SalegetData salegetData) {
        count = saleService.count(salegetData);
        if (salegetData.getTime().equals("月")) {
            saleSetData.setxAxis(months);
        } else {
            // 每次调用都创建新的列表，避免数据累积
            saleSetData.setxAxis(calculateDateRange(salegetData.getDate()));
        }
        saleSetData.setyAxis(count);
        return Result.buildSuccess(saleSetData);
    }

    // 计算指定日期前后7天的日期
    private List<String> calculateDateRange(Date date) {
        List<String> days = new ArrayList<>(); // 每次创建新的列表
        LocalDate inputDate = date.toLocalDate();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 计算前7天
        for (int i = 7; i > 0; i--) {
            LocalDate pastDate = inputDate.minusDays(i);
            days.add(pastDate.format(formatter));
        }

        // 添加当前日期
        days.add(inputDate.format(formatter));

        // 计算后7天
        for (int i = 1; i <= 7; i++) {
            LocalDate futureDate = inputDate.plusDays(i);
            days.add(futureDate.format(formatter));
        }

        return days;
    }
    @Operation(summary = "查询名称")
    @RequirePermission("报表查看")
    @PostMapping("/salefind")
    public Result salefind(String type) {
        if ((type.equals("种类"))) {
            return Result.buildSuccess(saleService.salefindByType());
        }
        else {
            return Result.buildSuccess(saleService.salefindBygoods());
        }
    }
    @Operation(summary = "订单名称")
    @PostMapping("/ordeBytime")
    public Result ordeBytime(@RequestBody OrderDto orderDto) {
        return Result.buildSuccess(saleService.salefindByName(orderDto));
    }
    @Operation(summary = "订单总量")
    @PostMapping("/allorder")
    public Result allorder(@RequestBody OrderDto orderDto) {
        return Result.buildSuccess(saleService.allorder(orderDto));
    }
}